#include "hdpushbuttons.h"

short _stdcall Inp32(short portaddr);
void _stdcall Out32(short portaddr, short datum);

HDPushButtons::HDPushButtons(QObject *parent)
: QThread(parent)
{
	start();
}

HDPushButtons::~HDPushButtons()
{

}

void HDPushButtons::run()
{
	pressedButtons.LeftThumbPressed=false;
	pressedButtons.RightThumbPressed=false;
	pressedButtons.LeftIndexPressed=false;
	pressedButtons.RightIndexPressed=false;
	pressedButtons.LeftMiddlePressed=false;
	pressedButtons.RightMiddlePressed=false;
	pressedButtons.LeftRingPressed=false;
	pressedButtons.RightRingPressed=false;

	timer = new QTimer();
	timer->setInterval(10);
	connect(timer, SIGNAL(timeout()), this, SLOT(ListenToButtons()));
	timer->start();

	Out32(0x378,255);

	// Start the event loop.
	this->exec();
}

void HDPushButtons::ListenToButtons()
{
	_input = Inp32(0x378);

	if((_input & 0x01)==0)
	{
		if(!pressedButtons.RightThumbPressed)
		{
			pressedButtons.RightThumbPressed=true;
			emit RightThumbToggled(true);
		}
	}
	else
	{
		if(pressedButtons.RightThumbPressed)
		{
			pressedButtons.RightThumbPressed=false;
			emit RightThumbToggled(false);
		}
	}

	if((_input & 0x02)==0)
	{
		if(!pressedButtons.RightIndexPressed)
		{
			pressedButtons.RightIndexPressed=true;
			emit RightIndexToggled(true);
		}
	}
	else
	{
		if(pressedButtons.RightIndexPressed)
		{
			pressedButtons.RightIndexPressed=false;
			emit RightIndexToggled(false);
		}
	}

	if((_input & 0x04)==0)
	{
		if(!pressedButtons.RightMiddlePressed)
		{
			pressedButtons.RightMiddlePressed=true;
			emit RightMiddleToggled(true);
		}
	}
	else
	{
		if(pressedButtons.RightMiddlePressed)
		{
			pressedButtons.RightMiddlePressed=false;
			emit RightMiddleToggled(false);
		}
	}

	if((_input & 0x08)==0)
	{
		if(!pressedButtons.RightRingPressed)
		{
			pressedButtons.RightRingPressed=true;
			emit RightRingToggled(true);
		}
	}
	else
	{
		if(pressedButtons.RightRingPressed)
		{
			pressedButtons.RightRingPressed=false;
			emit RightRingToggled(false);
		}
	}

	if((_input & 0x10)==0)
	{
		if(!pressedButtons.LeftThumbPressed)
		{
			pressedButtons.LeftThumbPressed=true;
			emit LeftThumbToggled(true);
		}
	}
	else
	{
		if(pressedButtons.LeftThumbPressed)
		{
			pressedButtons.LeftThumbPressed=false;
			emit LeftThumbToggled(false);
		}
	}

	if((_input & 0x20)==0)
	{
		if(!pressedButtons.LeftIndexPressed)
		{
			pressedButtons.LeftIndexPressed=true;
			emit LeftIndexToggled(true);
		}
	}
	else
	{
		if(pressedButtons.LeftIndexPressed)
		{
			pressedButtons.LeftIndexPressed=false;
			emit LeftIndexToggled(false);
		}
	}

	if((_input & 0x40)==0)
	{
		if(!pressedButtons.LeftMiddlePressed)
		{
			pressedButtons.LeftMiddlePressed=true;
			emit LeftMiddleToggled(true);
		}
	}
	else
	{
		if(pressedButtons.LeftMiddlePressed)
		{
			pressedButtons.LeftMiddlePressed=false;
			emit LeftMiddleToggled(false);
		}
	}

	if((_input & 0x80)==0)
	{
		if(!pressedButtons.LeftRingPressed)
		{
			pressedButtons.LeftRingPressed=true;
			emit LeftRingToggled(true);
		}
	}
	else
	{
		if(pressedButtons.LeftRingPressed)
		{
			pressedButtons.LeftRingPressed=false;
			emit LeftRingToggled(false);
		}
	}
}